#pragma once

#include <vector>

namespace solution_059
{

class Solution
{
public:
    std::vector< std::vector<int> > generateMatrix(int n)
    {
        std::vector matrix(n, std::vector(n, 0));
        const int round = (n + 1) / 2;
        int row, col, num = 0;
        for (int diag = 0; diag < round; ++diag) {
            for (row = diag,     col = diag;   col <= n-diag-1; ++col) matrix[row][col] = ++num;
            for (col = n-diag-1, row = diag+1; row <= n-diag-2; ++row) matrix[row][col] = ++num;
            if (diag != n - diag - 1) {
                for (row = n-diag-1, col = n-diag-1; col >= diag;   --col) matrix[row][col] = ++num;
                for (col = diag,     row = n-diag-2; row >= diag+1; --row) matrix[row][col] = ++num;
            }
        }
        return matrix;
    }
};

}  /* namespace solution_048 */
